package com.tdam2014.grupo3.flickr;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import org.json.JSONException;

import com.tdam2014.grupo3.flickr.util.URLUtils;

/**
 * Transport implementation using the REST interface.
 *
 * @author Anthony Eden
 */
public class REST extends Transport {
	
    public static final String PATH = "/services/rest/";
    


    /**
     * Construct a new REST transport instance.
     */
    public REST() {
        setTransportType(REST);
        setHost(Flickr.DEFAULT_API_HOST);
        setPath(PATH);
        setResponseClass(RESTResponse.class);
    }

    /**
     * Construct a new REST transport instance using the specified host endpoint.
     *
     * @param host The host endpoint
     */
    public REST(String host) {
        this();
        setHost(host);
    }

    /**
     * Construct a new REST transport instance using the specified host and port endpoint.
     *
     * @param host The host endpoint
     * @param port The port
     */
    public REST(String host, int port) {
        this();
        setHost(host);
        setPort(port);
    }

    /**
     * Invoke an HTTP GET request on a remote host.  You must close the InputStream after you are done with.
     *
     * @param path The request path
     * @param parameters The parameters (collection of Parameter objects)
     * @return The Response
     * @throws IOException
     * @throws JSONException
     */
    public Response get(String path, List<Parameter> parameters) throws IOException, JSONException {
        parameters.add(new Parameter("nojsoncallback", "1"));
        parameters.add(new Parameter("format", "json"));
        String data = getLine(path, parameters);
        return new RESTResponse(data);
    }
    
    private InputStream getInputStream(String path, List<Parameter> parameters) throws IOException {
        URL url = URLUtils.buildUrl(getHost(), getPort(), path, parameters);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.addRequestProperty("Cache-Control", "no-cache,max-age=0"); 
        conn.addRequestProperty("Pragma", "no-cache"); 
        conn.setRequestMethod("GET");
        conn.connect();
        return conn.getInputStream();
    }

    /**
     * Send a GET request to the provided URL with the given parameters, 
     * then return the response as a String.
     * 
     * @param path
     * @param parameters
     * @return the data in String
     * @throws IOException
     */
    public String getLine(String path, List<Parameter> parameters) throws IOException {
        InputStream in = null;
        BufferedReader rd = null;
        try {
            in = getInputStream(path, parameters);
            rd = new BufferedReader(new InputStreamReader(in));
            final StringBuffer buf = new StringBuffer();
            
            String line;
            while ((line = rd.readLine()) != null) {
                buf.append(line);
            }
            
            return buf.toString();
        } finally {
        	in.close();
        	rd.close();
        }
    }
    
	@Override
	protected Response post(String path, List<Parameter> parameters)
			throws IOException, JSONException {
		return null;
	}
}
